﻿using CNotebook;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
namespace NotebookS2
{
    public partial class ReportConfiguration : RibbonForm
    {
        public ReportConfiguration()
        {
            InitializeComponent();
        }

        public DataTable ReportTable { get; set; }
        public string ConnectionString { get; set; }

        public void BindReports()
        {
            ReportConfigManager oManager = new ReportConfigManager();
            DataTable dt =new DataTable();
            try
            {
                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();
                dt = oManager.GetReports();
                oManager.Close();

                this.ReportTable = dt;
                gridControl1.DataSource = this.ReportTable;
                gridControl1.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                dt.Dispose();
            }
        }

        private void ReportConfiguration_Load(object sender, EventArgs e)
        {
            BindReports();

            gridView1.ExpandAllGroups();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("Are you sure you want to update the configuration?", "Change Config", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                ReportConfigManager oManager = new ReportConfigManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                oManager.UpdateReports(this.ReportTable);

                oManager.Close();


                MessageBox.Show("Configuration successfully updated!!!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void txtReportFile_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            openFileDialog1.FileName = "";

            DialogResult result = openFileDialog1.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {

                string file = openFileDialog1.FileName;

                gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["ReportFile"], file);

            }
        }
    }
}
